<header class="mui-bar mui-bar-nav">
    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
    <h1 class="mui-title">下载</h1>
    <link rel="stylesheet" type="text/css" href="css/mui.css"/>
</header>
<div class="mui-content">
    <div class="mui-progressbar" id="bar" style="height: 100px;">
        <span style="height: 100%;"></span>
    </div>
    <button class="mui-btn mui-btn-block mui-btn-green" onclick="createDownload();">开始下载</button>
</div>
<script src="js/mui.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
//初始化下载对象
//var dtask = null;
//var loadUrl="http://1.su.bdimg.com/icon/weather/a0.jpg";
var loadUrl="http://snip.qq.com/resources/Snip_V2.0_5771.dmg";
var loadId="img1"
var filename = loadUrl.substring(loadUrl.lastIndexOf("/") + 1, loadUrl.length);
var relativePath = "_downloads/" + filename;
function createDownload(){
	//判断文件是否存在
//	plus.io.resolveLocalFileSystemURL(relativePath,
//		function(entry){
//		    if(entry.isFile){
//		        mui.toast('该文件已经下载过了');
//		        mui.alert(entry.fullPath);
//		    }
//	    },
//	    function(e){
//	    	dBase();
//	    }
//  )
	dBase();
}
function dBase(){
//  if(dtask){
//      mui.toast('该文件正在下载');
//      return;
//  }
    var dtask = plus.downloader.createDownload("http://1.su.bdimg.com/icon/weather/a0.jpg",{},function(d,status){
//      mui.toast(d.filename);
//  });
//  dtask.addEventListener("statechanged",function(task,status){
//      if(!dtask){return;}
//      switch(task.state){
//          case 1: mui.toast('开始下载...');break;//开始
//          case 2: mui.toast('链接到服务器...');break;//链接到服务器
//          case 3:
//              var progressVal = (task.downloadedSize/task.totalSize)*100;
//                  mui('#bar').progressbar({progress:progressVal}).show();
//              break;
//          case 4: mui.toast('下载完成');break;
//      }
		if ( status == 200 ) { 
			alert( "Download success: " + d.filename );
			plus.runtime.openFile('_downloads/'+d.filename);
		} else {
			 alert( "Download failed: " + status ); 
		} 
    });
    dtask.start();
}
</script>